import api from './index'
import { axios } from '@/utils/request'

/**
 * login func
 * parameter: {
 *     userName: '',
 *     password: '',
 *     remember_me: true,
 *     captcha: '12345'
 * }
 * @param parameter
 * @returns {*}
 */
export async function login(parameter) {
  var formData = new FormData(); // Currently empty
  for(let key in parameter){
    formData.append(key, parameter[key]);
  }
  formData.append('userType', 1);
  return axios({
    url:  window._CONFIG['domianAuthURL'] +'/oauth/token',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function phoneLogin(parameter) {
  return axios({
    url: '/sys/phoneLogin',
    method: 'post',
    data: parameter
  })
}

export function getSmsCaptcha(parameter) {
  return axios({
    url: api.SendSms,
    method: 'post',
    data: parameter
  })
}

export function getInfo() {
  return axios({
    url:  window._CONFIG['domianURL'] +'/cnUserController/getNowUser',
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function logout(logoutToken) {
  return axios({
    url:  window._CONFIG['domianURL'] +'/loginController/loginOut',
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      'X-Access-Token':  logoutToken
    }
  })
}

/**
 * 第三方登录
 * @param token
 * @returns {*}
 */
export function thirdLogin(token) {
  return axios({
    url: `/thirdLogin/getLoginUser/${token}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}
